
 /**  
 * Project Name:zhonghecrm  
 * File Name:SysMenuServiceImpl.java  
 * Package Name:com.crm.service.impl  
 * Date:2016年10月26日下午2:41:35   
 *  
*/  
  
package com.crm.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.cache.ICache;
import com.crm.cache.MainCacheUtil;
import com.crm.dao.SysMenuMapper;
import com.crm.model.SysMenu;
import com.crm.service.SysMenuService;
import com.crm.util.UUIDUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**  
 * ClassName:SysMenuServiceImpl <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年10月26日 下午2:41:35 <br/>  
 * @author   hanlinxiu  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu> implements SysMenuService{
	
	@Autowired
	SysMenuMapper sysMenuMapper; 
	
	@Autowired
	private ICache redisCache;
	
	@Override
	public int insert(SysMenu record) {
		
		String uuid = UUIDUtil.getUUID();
		record.setMenuId(uuid);
		
		if(ValidateUtil.isEmpty(record.getParentId())){
			Integer maxSortCode = sysMenuMapper.selectMaxSortCode(new SysMenu());
			if(maxSortCode == null)
				maxSortCode = 0;
			record.setSortCode((maxSortCode/100000+1)*100000);
			record.setParentId("0");
		}else{
			SysMenu parentMenu = sysMenuMapper.selectByPrimaryKey(record.getParentId());
			
			int pSortCode = parentMenu.getSortCode();
			int b = 1;
			while(true){
				double c = Math.pow(5, b);
				if(pSortCode % c != 0)
					break;
				b++;
			}
			
			SysMenu t = new SysMenu();
			t.setParentId(parentMenu.getMenuId());
			Integer maxSortCode = sysMenuMapper.selectMaxSortCode(t);
			if(maxSortCode == null)
				maxSortCode = pSortCode;
			
			record.setSortCode(maxSortCode + new Double(Math.pow(10, b-2)).intValue());
		}
		
		int i = super.insert(record);
		
		Page<SysMenu> page = new Page<>();
		SysMenu sysMenu = new SysMenu();
		redisCache.remove(MainCacheUtil.scpoe, "sys_menus");
		redisCache.put(MainCacheUtil.scpoe, "sys_menus", this.selectAll(page, sysMenu));
		
		return i;
	}
	
	
	
	@Override
	public int updateByPrimaryKey(SysMenu record) {
		
		int i = super.updateByPrimaryKey(record);
		
		Page<SysMenu> page = new Page<>();
		SysMenu sysMenu = new SysMenu();
		redisCache.remove(MainCacheUtil.scpoe, "sys_menus");
		redisCache.put(MainCacheUtil.scpoe, "sys_menus", this.selectAll(page, sysMenu));
		
		return i;
	}
	

	/**  
	 * TODO 查询树形结构，model未添加isleaf
	 * @see com.crm.service.impl.BaseServiceImpl#selectAll(com.github.pagehelper.Page, com.crm.model.BaseModel)  
	 */
	@Override
	public List<SysMenu> selectAll(Page<SysMenu> page, SysMenu t) {
		
		List<SysMenu> list = new ArrayList<>();
		
		list = redisCache.getObjectList(MainCacheUtil.scpoe, "sys_menus", SysMenu.class);
		
		if(null == list){
			
			PageHelper.startPage(page.getPageNum(), page.getPageSize(), "sort_code asc");
			
			t.setParentId("0");
			list = sysMenuMapper.selectAllByWhere(t);
			
			list = this.test(page, list);
			
			redisCache.put(MainCacheUtil.scpoe, "sys_menus", list);
		}
		
		return list;
	}
	
	//递归测试
	public List<SysMenu> test(Page<SysMenu> page, List<SysMenu> list){
		
		for(SysMenu sysMenu : list){
			
			SysMenu t = new SysMenu();
			t.setEnable(null);
			t.setParentId(sysMenu.getMenuId());
			
			PageHelper.startPage(page.getPageNum(), page.getPageSize(), "sort_code asc");
			List<SysMenu> childsList = sysMenuMapper.selectAllByWhere(t);
			
			sysMenu.setChilds(childsList);
			
			if(!sysMenuMapper.selectAllByWhere(t).isEmpty()){
				test(page, childsList);
			}
			
		}
		
		return list;
	}

	@Override
	public List<SysMenu> selectForSort(Page<SysMenu> page, SysMenu sysMenu) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "sort_code asc");
		  
		List<SysMenu> list = sysMenuMapper.selectAllByWhere(sysMenu);
		
		return list;
		
	}

	@Override
	public void sortMenu(SysMenu record, String refMenuId, Integer order) {
		  
		record  = sysMenuMapper.selectByPrimaryKey(record.getMenuId());
		
		SysMenu refRecord = new SysMenu();
		refRecord.setMenuId(refMenuId);
		
		if(ValidateUtil.isEmpty(record.getParentId())){
			
			
			
		}else{
			
			SysMenu t = new SysMenu();
			t.setParentId(record.getParentId());
			
			SysMenu parentMenu = sysMenuMapper.selectByPrimaryKey(record.getParentId());
			
			LinkedList<SysMenu> preList = (LinkedList<SysMenu>)sysMenuMapper.selectAllByWhere(t); 
			preList.remove(record);
			
			int refIndex = preList.indexOf(refRecord);
			
			//1-放到菜单的前面,2-放到参考菜单的后面
			if(order == 1){
				preList.add(refIndex, record);
			}else if(order == 2){
				preList.add(refIndex+1, record);
			}else{
				return;
			}
			
			//求排序码后面有几个0
			int b = 1;
			int sortCode = parentMenu.getSortCode();
			while(true){
				double c = Math.pow(5, b);
				if(sortCode % c != 0)
					break;
				b++;
			}
			
			for(int i=0; i<preList.size(); i++){
				preList.get(i).setSortCode(sortCode + new Double(Math.pow(10, b-2)*(i+1)).intValue());	
			}
			
		}
		
	}

	@Override
	public List<SysMenu> selectAll(SysMenu reocord) {
		
		return sysMenuMapper.selectAllByWhere(reocord);
	}

}
  
